//
//  CVideoView.m
//  SinaWebo
//
//  Created by wwj on 16/7/7.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "CVideoView.h"
#import "JPVideoPlayer.h"
#import "PicMetadata.h"
#import "MediaInfo.h"
#import "PageInfo.h"
#import "PicInfo.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>

@interface CVideoView ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak, nonatomic) IBOutlet UILabel *coutLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLable;
@end

@implementation CVideoView

+ (instancetype)videoView{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    self.autoresizingMask = UIViewAutoresizingNone;
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [super touchesBegan:touches withEvent:event];
//    MediaInfo *mediaInfo = _pageInfo.media_info;
//    [DCURLRouter pushURLString:@"sinaweibo://home/video/play" query:@{@"params": mediaInfo.stream_url} animated:YES];
//}


- (void)setPageInfo:(PageInfo *)pageInfo{
    _pageInfo = pageInfo;
    PicMetadata *picMetaData = _pageInfo.pic_info.pic_middle;
    self.button.hidden = NO;
    self.coutLabel.hidden = NO;
    self.timeLable.hidden = NO;
    MediaInfo *mediaInfo = _pageInfo.media_info;
    _coutLabel.text = mediaInfo.online_users;
    _timeLable.text = [NSString stringWithFormat:@"%02zd:%02zd", mediaInfo.duration / 60, mediaInfo.duration %60];
    [_imageView sd_setImageWithURL:[NSURL URLWithString:picMetaData.url]];
    
//    [self playVideo];
}

- (void)stopVideo{
    [[JPVideoPlayer sharedInstance] stop];
}

- (void)playVideo{
    MediaInfo *mediaInfo = _pageInfo.media_info;
    JPVideoPlayer *player = [JPVideoPlayer sharedInstance];
    [player playWithUrl:[NSURL URLWithString:mediaInfo.stream_url] showView:self.imageView];
    player.mute = NO;
}



@end
